@extends('layout.adminlayout')
@section('css')
    <script src="/admin/js/metisMenu.min.js"></script>
    <script src="/admin/js/custom.js"></script>
    <link href="/admin/css/area_info.css" rel="stylesheet">
    <link href="/admin/css/custom.css" rel="stylesheet">
    <link href="/admin/css/add_manage.css" rel="stylesheet">
    <link href="/admin/css/select_building.css" rel="stylesheet">
@endsection
@section('content')
<div class="forms">
      <div class=" form-grids row form-grids-right"> 
       <div class="widget-shadow " data-example-id="basic-forms"> 
        <div class="form-title"> 
         <h4>编辑设备 :</h4>
        </div> 
        <div class="form-body"> 
         <form action="/admin/building/store" method="post" class="form-horizontal" onsubmit="return fun4()">
             {{csrf_field()}}
          <div class="form-group">
           <label for="inputPassword3" class="col-sm-2 control-label" style="width:33%;float: left;top: 30px;"> DevEUI: </label> 
           <div class="col-sm-3"> 
            <input type="text" class="form-control" name="eui" id="eui" value="{{$data['eui']}}" autocomplete="off" readonly="" style="width: 70%;float: right;" />
            <input type="hidden" name="" id="rd_id" value="{{$data['id']}}">
            <input type="text" name="notautosubmit" style="display:none" />
           </div> 
          </div>
          <div class="form-group"> 
           <label for="inputEmail3" class="col-sm-2 control-label" style="width:33%;float: left;top: 30px;"> 区域 : </label> 
           <li class="area_info"> 
            <div class="col-sm-3"> 
             <select name="area" id="area" class="form-control1" form-control1="" style="width: 70%;float: right;">
                 <option value="0">请选择区域</option>
                 @if($area != null)
                     @foreach($area as $v)
                         <option value="{{$v['a_id']}}"
                                 @if($v['a_id'] == $data['a_id'])
                                 selected
                                 @endif
                         >{{$v['a_name']}}</option>
                     @endforeach
                 @endif
             </select>
            </div> </li> 
          </div> 
          <div class="form-group"> 
           <label for="inputEmail3" class="col-sm-2 control-label" style="width:33%;float: left;    top: 30px;"> 建筑 : </label> 
           <li class="area_info"> 
            <div class="col-sm-3"> 
             <select name="building" id="building" class="form-control1" style="width: 70%;float: right;">
                 <option value="0">请选择建筑</option>
                 @if($building != null)
                     @foreach($building as $v)
                         <option value="{{$v['b_id']}}"
                                 @if($v['b_id'] == $data['b_id'])
                                 selected
                                 @endif
                         >{{$v['b_name']}}</option>
                     @endforeach
                 @endif
             </select>
            </div> </li> 
          </div>
          <div class="form-group"> 
           <label for="inputEmail3" class="col-sm-2 control-label" style="width:33%;float: left;    top: 30px;"> 房间 : </label> 
           <li class="area_info"> 
            <div class="col-sm-3"> 
             <select name="room" id="rooms" class="form-control1" style="width: 70%;float: right;">
                 <option value="0">请选择房间</option>
                 @if($room != null)
                     @foreach($room as $v)
                         <option value="{{$v['r_id']}}"
                                 @if($v['r_id'] == $data['r_id'])
                                 selected
                                 @endif
                         >{{$v['r_name']}}</option>
                     @endforeach
                 @endif
             </select>
            </div> </li> 
          </div> 
          <div class="form-group"> 
           <label for="inputEmail3" class="col-sm-2 control-label" style="width:33%;float: left;    top: 30px;"> 具体位置 : </label> 
           <div class="col-sm-3"> 
            <input type="text" class="form-control" name="loc" placeholder="具体位置" id="loc" style="width: 70%;float: right;" value="{{$data['location']}}"/>
           </div> 
          </div> 
          <div class="form-group"> 
           <label for="inputEmail3" class="col-sm-2 control-label" style="width:33%;float: left;    top: 30px;"> 所属类型 : </label> 
           <li class="area_info"> 
            <div class="col-sm-3">
            <input type="hidden" id="fri_type" name="fri_type" value="{{$data['type_id']}}"> 
             <select name="type" id="type" class="form-control1" style="width: 70%;float: right;">
                 <option value="0">请选择设备类型</option>
                 @if($type != null)
                     @foreach($type as $val)
                         <option value="{{$val['id']}}"
                                 @if($val['id'] == $data['type_id'])
                                 selected
                                 @endif
                         >{{$val['type']}}</option>
                     @endforeach
                 @endif
             </select>
            </div> </li> 
          </div> 
          <div class="form-group"> 
           <label for="inputEmail3" class="col-sm-2 control-label" style="width:33%;float: left;    top: 30px;"> 设备型号 : </label> 
           <li class="area_info"> 
            <div class="col-sm-3"> 
             <select name="model" id="model" class="form-control1" style="width: 70%;float: right;">
                 <option value="0">请选择设备型号</option>
                 @if($model != null)
                     @foreach($model as $v)
                         <option value="{{$v['tp_id']}}"
                                 @if($v['tp_id'] == $data['model_id'])
                                 selected
                                 @endif
                         >{{$v['t_model']}}</option>
                     @endforeach
                 @endif
             </select>
            </div> </li> 
          </div> 
          <div class="col-sm-offset-2">
           <button type="button" class="btn btn-default" id="sub" style="margin-left: -22px;width: 90%;">提交</button> 
          </div> 
         </form> 
        </div> 
       </div> 
      </div> 
     </div> 
<script>
    //当点击eui时,dv卷出
    $('#eui').click(function () {
        var display = $('#dv').css('display')
        if(display == 'none'){
            $('#dv').slideDown();
            $('#eui').attr('readonly',false);
        }else{
            $('#dv').slideUp();
            $('#eui').attr('readonly',true);
        }
    });
    //鼠标移入移出
    $('.st_this').mouseover(function () {
        $(this).addClass('a');
    });
    $('.st_this').mouseout(function () {
        $(this).removeClass('a');
    });
    //点击修改样式
    $('.st_this').click(function () {
        var select = $(this).html();
        $('#eui').val(select);
        $('.st_this').removeClass('b');
        $(this).addClass('b');
        $('#dv').slideUp();
    });
    //输入匹配
    $('#eui').keyup(function () {
        $('.st_this').attr('hidden',true);
        var val = $('#eui').val();
        var preg = new RegExp(val + "$","gim");
        $('.st_this').each(function () {
            var res = $(this).html();
            if(preg.test(res)){
                $(this).attr('hidden',false);
            }
            preg.test(res);
        })
    })
  //区域建筑联动
  $('#area').change(function () {
    var areaId = $('#area').val()
    $.ajax({
      type:'post',
      url:'/admin/room/getbuild',
            async:false,
            data:{
                "_token": $("input[name=_token]").val(),
                'areaId':areaId,
            },
            dataType:'json',
            success:function (data) {
                var str = '<option class="dstyle" value="">请选择建筑</option>';
                for(var i=0;i<data.length;i++){
                    str += '<option class="dstyle" value="'+data[i]['b_id']+'">'+data[i]['b_name']+'</option>';
        }
                $('#building').empty();
                $('#building').append(str);
                $('#rooms').empty();
                $('#rooms').append('<option value="0">请选择房间</option>');
            }
    })
    })

  //建筑房间联动
     $('#building').change(function () {
        var buildingId = $('#building').val();
        $.ajax({
            type:'post',
            url:'/admin/room/getroom',
            async:false,
            data:{
                "_token": $("input[name=_token]").val(),
                'buildingId':buildingId,
            },
            dataType:'json',
            success:function (data) {
                var option = '<option value="0">请选择房间</option>';
                for(var i=0;i<data.length;i++){
                    console.log(data[i]['r_id']);
                    option += '<option value="'+data[i]['r_id']+'">'+data[i]['r_name']+'</option>';
                }
                $('#rooms').empty();
                $('#rooms').append(option);
            }
        });
    });

  //类型与型号联动
    $('#type').change(function () {
        var typeId = $('#type').val();
        $.ajax({
            type:'post',
            url:'/admin/device/getmodel',
            async:false,
            data:{
                "_token": $("input[name=_token]").val(),
                'typeId':typeId,
            },
            dataType:'json',
            success:function (data) {
                var option = '<option value="0">请选择型号</option>';
                for(var i=0;i<data.length;i++){
                    console.log(data[i]['tp_id']);
                    option += '<option value="'+data[i]['tp_id']+'">'+data[i]['t_model']+'</option>';
                }
                $('#model').empty();
                $('#model').append(option);
            }
        });
    });
    //提交表单
    $('#sub').click(function () {
        var rd_id = $('#rd_id').val();
        var eui = $('#eui').val();
        var area = $('#area').val();
        var building = $('#building').val();
        var rooms = $('#rooms').val();
        var loc = $('#loc').val();
        var type = $('#type').val();
        var fri_type = $('#fri_type').val();
        var model = $('#model').val();
        if(eui != undefined && area != 0 && building !=0 && rooms != 0 && type != 0 && model != 0){
            $.ajax({
                type:'post',
                url:'/admin/device/edit',
                async:false,
                data:{
                    "_token": $("input[name=_token]").val(),
                    'rd_id':rd_id,
                    'eui':eui,
                    'area':area,
                    'building':building,
                    'rooms':rooms,
                    'loc':loc,
                    'type':type,
                    'fri_type':fri_type,
                    'model':model
                },
                dataType:'json',
                success:function (data) {
                    location.href = '/admin/device/index';
                }
            })
        }else{
            alert('信息填写不完整或经纬度填写不合法')
            return false;
        }
    });
</script>
@endsection
@section('lastjs')
    <script src="/admin/js/add_manage.js"></script>
    <!-- <script src="/admin/js/select_building.js"></script> -->
@endsection